package com.woniu.mall.service.impl;

import com.woniu.mall.constant.PlatformRedisKey;
import com.woniu.mall.pojo.Coupon;
import com.woniu.mall.service.CouponPlatformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Random;

/**
 * 1. @description:
 * 2. @author: Lingger
 * 3. @time: 2023/5/22
 */

@Service
public class CouponPlatformServiceImpl implements CouponPlatformService {

    @Autowired
    private RedisTemplate redisTemplate;

    //新增分区优惠券
    @Override
    public Boolean saveZoneCoupon(Coupon coupon) {
        boolean flag = false;
        BigDecimal amount;
        switch (coupon.getAmount().intValue()) {
            case PlatformRedisKey.AMOUNT_5:
                amount = new BigDecimal(PlatformRedisKey.AMOUNT_5);
                break;
            case PlatformRedisKey.AMOUNT_10:
                amount = new BigDecimal(PlatformRedisKey.AMOUNT_10);
                break;
            case PlatformRedisKey.AMOUNT_20:
                amount = new BigDecimal(PlatformRedisKey.AMOUNT_20);
                break;
            case PlatformRedisKey.AMOUNT_50:
                amount = new BigDecimal(PlatformRedisKey.AMOUNT_50);
                break;
            case PlatformRedisKey.AMOUNT_100:
                amount = new BigDecimal(PlatformRedisKey.AMOUNT_100);
                break;
            default:
                return flag;
        }

        String firstName = startWith(coupon.getName());

        //是否为酒店民宿优惠劵
        if (firstName.equals(PlatformRedisKey.HOTELS_HOMESTAYS)) {
            redisTemplate.boundHashOps(PlatformRedisKey.Zone_COUPON).
                    put(PlatformRedisKey.HOTELS_HOMESTAYS + amount, amount);
            flag = true;
        }
        //是否为休闲玩乐优惠券
        if (firstName.equals(PlatformRedisKey.LEISURE_PLAY)) {
            redisTemplate.boundHashOps(PlatformRedisKey.Zone_COUPON).
                    put(PlatformRedisKey.LEISURE_PLAY + amount, amount);
            flag = true;
        }
        //是否为饮品小吃优惠券
        if (firstName.equals(PlatformRedisKey.BEVERAGES_SNACKS)) {
            redisTemplate.boundHashOps(PlatformRedisKey.Zone_COUPON).
                    put(PlatformRedisKey.BEVERAGES_SNACKS + amount, amount);
            flag = true;
        }
        //是否为超市便利优惠券
        if (firstName.equals(PlatformRedisKey.SUPERMARKET_CONVENIENCE)) {
            redisTemplate.boundHashOps(PlatformRedisKey.Zone_COUPON).
                    put(PlatformRedisKey.SUPERMARKET_CONVENIENCE + amount, amount);
            flag = true;
        }

        //是否为品质百货优惠券
        if (firstName.equals(PlatformRedisKey.QUALITY_DEPARTMENT)) {
            redisTemplate.boundHashOps(PlatformRedisKey.Zone_COUPON).
                    put(PlatformRedisKey.QUALITY_DEPARTMENT + amount, amount);
            flag = true;
        }

        //是否为看病买药优惠券
        if (firstName.equals(PlatformRedisKey.BUY_MEDICINE)) {
            redisTemplate.boundHashOps(PlatformRedisKey.Zone_COUPON).
                    put(PlatformRedisKey.BUY_MEDICINE + amount, amount);
            flag = true;
        }

        return flag;
    }

    //新增天天神券
    @Override
    public Coupon saveTianTian() {
        Coupon coupon = new Coupon();

        coupon.setName(PlatformRedisKey.TIANTIAN_COUPON);
        coupon.setStatus(true);
        coupon.setStartingAmount(new BigDecimal(PlatformRedisKey.STARTING_AMOUNT
                + new Random().nextInt(PlatformRedisKey.MIN_STARTING_AMOUNT)));
        coupon.setAmount(new BigDecimal(PlatformRedisKey.MIN_AMOUNT + new Random().nextInt(PlatformRedisKey.RANDOM_AMOUNT)));
        return coupon;
    }

    //新增红包
    @Override
    public Coupon saveRedEnvelope() {
        Coupon coupon = new Coupon();
        coupon.setName(PlatformRedisKey.RED_ENVELOPE);
        coupon.setAmount(new BigDecimal(PlatformRedisKey.AMOUNT_5));
        coupon.setNumber(PlatformRedisKey.NUMBER);
        return coupon;
    }

    //删除分区优惠券
    @Override
    public Long deleteZone(String couponName, BigDecimal amount) {

        Long number = redisTemplate.boundHashOps(PlatformRedisKey.Zone_COUPON).delete(couponName + ":" + amount);
        return number;
    }

    private String startWith(String name) {
        String[] split = name.split(":");
        String startWith = split[0] + ":";
        return startWith;
    }
}
